package adminDynamicEnumService

import (
	"encoding/json"

	"gitee.com/krio/ginc/example/core/exerr"
	exampleDtoApiPbAdminDynamicEnum "gitee.com/krio/ginc/example/dto/api/pb/admin/dynamic/enum"
	adminDynamicEnumDao "gitee.com/krio/ginc/example/internal/admin/dynamic/enum/dao"
	adminDynamicEnumModel "gitee.com/krio/ginc/example/internal/admin/dynamic/enum/model"
	commonEntry "gitee.com/krio/ginc/example/internal/common/entry/common"
	commonModel "gitee.com/krio/ginc/example/internal/common/model/common"
	"github.com/gin-gonic/gin"
)

// DynamicEnumList 列表
func DynamicEnumList(ctx *gin.Context, req *exampleDtoApiPbAdminDynamicEnum.DynamicEnumListReq) (*exampleDtoApiPbAdminDynamicEnum.DynamicEnumListResp, error) {
	resp := &exampleDtoApiPbAdminDynamicEnum.DynamicEnumListResp{}
	search := struct {
		Keyword string `json:"keyword"`
	}{}
	if req.Keywords != "" {
		err := json.Unmarshal([]byte(req.Keywords), &search)
		if err != nil {
			return nil, err
		}
	}
	dao := &adminDynamicEnumDao.DynamicEnumDao{}
	list, total, err := dao.DynamicEnumList(ctx, &commonEntry.ListCommonReq{
		Page:    req.Page,
		Limit:   req.Limit,
		Keyword: search.Keyword,
		OrderBy: []*commonEntry.OrderBy{
			{
				IsDesc: true,
				Field:  "id",
			},
		},
	})
	if err != nil {
		return nil, err
	}
	var data []*exampleDtoApiPbAdminDynamicEnum.DynamicEnumInfo
	for _, v := range list {
		data = append(data, formatDetail(v))
	}
	resp.Total = total
	resp.List = data
	return resp, nil
}

// DynamicEnumDetail 详情
func DynamicEnumDetail(ctx *gin.Context, req *exampleDtoApiPbAdminDynamicEnum.DynamicEnumDetailReq) (*exampleDtoApiPbAdminDynamicEnum.DynamicEnumInfo, error) {
	dao := &adminDynamicEnumDao.DynamicEnumDao{}
	detail, has, err := dao.DynamicEnumDetail(ctx, req.Id)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, exerr.DataNotFound
	}

	return formatDetail(detail), nil
}

// DynamicEnumAdd 新增
func DynamicEnumAdd(ctx *gin.Context, req *exampleDtoApiPbAdminDynamicEnum.DynamicEnumInfo) (*exampleDtoApiPbAdminDynamicEnum.DynamicEnumAddResp, error) {
	one := &adminDynamicEnumModel.DynamicEnum{
		Basic:  commonModel.Basic{},
		Label:  req.Label,
		Key:    req.Key,
		Remark: req.Remark,
		Status: commonModel.DataStatus(req.Status),
	}
	dao := &adminDynamicEnumDao.DynamicEnumDao{}
	id, err := dao.DynamicEnumAdd(ctx, one)
	if err != nil {
		return nil, err
	}
	return &exampleDtoApiPbAdminDynamicEnum.DynamicEnumAddResp{
		Id: id,
	}, nil
}

// DynamicEnumUpdate 修改
func DynamicEnumUpdate(ctx *gin.Context, req *exampleDtoApiPbAdminDynamicEnum.DynamicEnumInfo) (*exampleDtoApiPbAdminDynamicEnum.DynamicEnumAddResp, error) {
	dao := &adminDynamicEnumDao.DynamicEnumDao{}
	_, err := dao.DynamicEnumUpdate(ctx, &adminDynamicEnumModel.DynamicEnum{
		Basic:  commonModel.Basic{ID: req.Id},
		Label:  req.Label,
		Key:    req.Key,
		Remark: req.Remark,
		Status: commonModel.DataStatus(req.Status),
	})
	if err != nil {
		return nil, err
	}
	return &exampleDtoApiPbAdminDynamicEnum.DynamicEnumAddResp{}, nil
}

// DynamicEnumDelete 删除
func DynamicEnumDelete(ctx *gin.Context, req *exampleDtoApiPbAdminDynamicEnum.DynamicEnumDeleteReq) (*exampleDtoApiPbAdminDynamicEnum.DynamicEnumDeleteResp, error) {
	dao := &adminDynamicEnumDao.DynamicEnumDao{}
	err := dao.DynamicEnumDelete(ctx, req.Ids)
	if err != nil {
		return nil, err
	}
	return &exampleDtoApiPbAdminDynamicEnum.DynamicEnumDeleteResp{}, nil
}

// 数据装换
func formatDetail(v *adminDynamicEnumModel.DynamicEnum) *exampleDtoApiPbAdminDynamicEnum.DynamicEnumInfo {
	return &exampleDtoApiPbAdminDynamicEnum.DynamicEnumInfo{
		Id:         v.ID,
		Label:      v.Label,
		Key:        v.Key,
		Remark:     v.Remark,
		Status:     int32(v.Status),
		CreateTime: v.CreatedAt.Format("2006-01-02 15:04:05"),
		UpdateTime: v.UpdatedAt.Format("2006-01-02 15:04:05"),
	}
}
